package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.Result;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * 获取当前登录用户的id,为购物车对象赋值
     * 根据当前登录用户的id及本次添加菜品的id或者套餐id查询购物车数据是否存在
     * 如果存在,则在它的数量上加1
     * 如果不存在,则添加到购物车,数量默认就是1
     *
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody ShoppingCart shoppingCart) {

        //获取当前登录用户的id
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, currentId);

        //判断当前登录用户添加的是菜品还是套餐
        if (dishId != null) {
            wrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        //判断当前菜品和套餐在购物车是否存在,如果存在,则在数量上加1,如果不存在则添加到购物车中
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        if (one != null) {
            Integer number = one.getNumber();
            one.setNumber(number + 1);
            shoppingCartService.updateById(one);
        } else {
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            one = shoppingCart;
        }

        return Result.success(one);

    }


    /**
     * 将套餐/菜品的数量减少为1,
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public Result sub(@RequestBody ShoppingCart shoppingCart) {

        //获取当前登录用户的id
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, currentId);

        //判断当前用户减少的是菜品还是套餐
        if (dishId != null) {
            wrapper.eq(ShoppingCart::getDishId, dishId);
        }else{
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        //判断当前菜品/套餐的数量是否为0,如果为0就删除购物车记录
        ShoppingCart cart = shoppingCartService.getOne(wrapper);
        if (cart.getNumber() > 1){
            Integer number = cart.getNumber();
            cart.setNumber(number - 1);
            shoppingCartService.updateById(cart);
        }else {
            //根据菜品/套餐的id删除
            LambdaQueryWrapper<ShoppingCart> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(ShoppingCart::getId, cart.getId());
            shoppingCartService.remove(wrapper);
        }

        return Result.success(cart);
    }

    /**
     * 根据当前登录用户的id查询购物车信息
     * @return
     */
    @GetMapping("/list")
    public Result<List<ShoppingCart>> list(){
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, currentId);
        wrapper.orderByDesc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        return Result.success(list);
    }

    /**
     * 根据当前登录用户的id删除购物车
     * @return
     */
    @DeleteMapping("/clean")
    public Result clean(){
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, currentId);

        shoppingCartService.remove(wrapper);

        return Result.success();
    }







}
